# @Time : 2024-09-29 14:17
# @Author : Fioman 
# @Phone : 13149920693
"""
线阵相机的一些参数配置,就是原来的ConfigParams,设置为单例模式,全局可以使用.
"""
import threading

from common.produce_logger import ProduceLogger
from db_tools.db_handler import DbHandler

db = DbHandler()
logger = ProduceLogger()


class LineCameraParams(object):
    """
    数据库操作类
    """
    __instance = None
    __init_flag = False
    __lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            with cls.__lock:
                cls.__instance = super(LineCameraParams, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):
        if not self.__init_flag:
            print("ProduceLogger.__init__() 进行初始化")
            self.__init_flag = True
            self.reload_camera_params()

    def reload_camera_params(self):
        """
        重新加载线阵相机的参数
        :return:
        """
        result = db.get_camera_params()
        if result.state:
            params = result.data
            self.camera_width = params.get("camera_width")
            self.camera_height = params.get("camera_height")
            self.height_wid_ratio = params.get("height_wid_ratio")
            self.mm2pix = params.get("mm2pix")
            self.mid_joint_pos01 = params.get("mid_joint_pos01")
            self.mid_joint_pos02 = params.get("mid_joint_pos02")
            self.sensor_pos = params.get("sensor_pos")
            self.table_pos_start = params.get("table_pos_start")
            self.table_pos_end = params.get("table_pos_end")
            self.table_val_thres = params.get("table_val_thres")
            self.sensor_val_thres = params.get("sensor_val_thres")
            self.size_scale = params.get("size_scale")
            self.height_max = params.get("height_max")
            self.height_min = params.get("height_min")
            self.sensor_dis_error = params.get("sensor_dis_error")
            self.long_cropper_thres = params.get("long_cropper_thres", 100)
            self.calc_thres_big = params.get("calc_thres_big", 15)
            self.calc_thres_small = params.get("calc_thres_small", 10)
            self.first_board_error = params.get("first_board_error", 13)
            self.second_board_error = params.get("second_board_error", 13)
            self.total_board_error = params.get("total_board_error", 18)
            logger.debug(f"线扫视觉参数阈值加载: \n拍摄宽:{self.camera_width},拍摄高:{self.camera_height},"
                         f"像素比:{self.mm2pix},高宽比:{self.height_wid_ratio},拼接处1:{self.mid_joint_pos01},"
                         f"拼接处2:{self.mid_joint_pos02},传感器位置:{self.sensor_pos},台布起点:{self.table_pos_start},"
                         f"台布终点:{self.table_pos_end},台布阈值:{self.table_val_thres},传感器阈值:{self.sensor_val_thres},"
                         f"图像缩放比:{self.size_scale},物料最大高度:{self.height_max},物料最小高度:{self.height_min},"
                         f"拍照触发位误差阈值:{self.sensor_dis_error},长铜箔阈值:{self.long_cropper_thres},"
                         f"识别偏大阈值:{self.calc_thres_big},识别偏小阈值:{self.calc_thres_small},第一块阈值:"
                         f"{self.first_board_error},第二块阈值:{self.second_board_error},"
                         f"总长阈值:{self.total_board_error}")
        else:
            logger.debug(result.info)
